<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 5/19/14
 * Time: 8:10 PM
 */

namespace controller;

include_once("Entry.php");
include_once("Config.php");


class ControllerUtil
{
  private static $langPacks;

  private static function init()
  {
    $langs = Config::getPropertiesAsArray("lang");
    $keys = array_keys($langs);

    for ($i = 0; $i < count($langs); $i++) {
      $fileName = $langs[$keys[$i]];
      $file = file($fileName);
      self::$langPacks[$keys[$i]] = self::parse($file);
    }

  }

  private static function parse($parseArray)
  {
    $splMap = new \SplObjectStorage();
    $arrayLength = count($parseArray);
    for ($i = 0; $i < $arrayLength; $i++) {
      $message = new Entry();
      $key = self::getKey($parseArray[$i]);
      if ($key) {
        $message->setKey($key);
        $message->setContent(self::getMessage($parseArray[$i]));
        $splMap->attach($message);
      }
    }
    return $splMap;
  }

  static function getMessageByKey($key, $lang)
  {
    if (!self::$langPacks) {
      self::init();
    }

    $selectedPack = self::$langPacks[$lang];

    $selectedPack->rewind();
    $count = $selectedPack->count();
    for ($i = 0; $i < $count; $i++) {
      if ($selectedPack->current()->getKey() == trim($key)) {
        return $selectedPack->current()->getContent();
      }
      $selectedPack->next();
    }
  }

  static function getMessageByKeyWithParameter($key, $lang, $param)
  {
    $message = self::getMessageByKey($key, $lang);
    $message = self::setParam($param, $message);
    return $message;
  }

  private static function setParam($param, $message)
  {
    $keys = array_keys($param);
    foreach($keys as $key) {
      $message = str_replace("\${".$key."}", $param[$key], $message);
    }
    return $message;
  }

  static function getKey($parseString)
  {
    $keyEndPos = stripos($parseString, "=");
    if (!$keyEndPos) {
      return false;
    }
    $key = trim(substr($parseString, 0, $keyEndPos));
    return $key;
  }

  static function getMessage($parseString)
  {
    $messageStartPos = stripos($parseString, "=") + 1;
    if (!$messageStartPos) {
      return false;
    }
    $message = trim(substr($parseString, $messageStartPos, strlen($parseString)));
    return $message;
  }


  static function getActionFromUrl($url)
  {
    $action = "";
    $startActionPos = stripos($url, "?action=");
    if( $startActionPos < 0) {
      return "index";
    } else {
      $startActionPos += 8;
    }
    $endActionPos = stripos($url, "&", $startActionPos);
    if (empty($endActionPos)) {
      $action = substr($url, $startActionPos);
    } else {
      $action = substr($url, $startActionPos, $endActionPos - $startActionPos);
    }
    return $action;
  }

  static function getEncodedActionFromUrlWithParameters($url)
  {
    $startActionPos = stripos($url, "?action=");
    if ($startActionPos >= 0) {
      $startActionPos += 8;
    } else {
      return "index";
    }
    $action = substr($url, $startActionPos);
    $action = self::encodeUrl($action);
    return $action;
  }

  static function encodeUrl($url)
  {
    $url = str_replace("&", "$", $url);
    $url = str_replace("#", "|", $url);
    return $url;
  }

  static function decodeUrl($url)
  {
    $url = str_replace("$", "&", $url);
    $url = str_replace("|", "#", $url);
    return $url;
  }

}